package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.photomovie.PhotoMovieFactory;
import com.stark.photomovie.render.GLTextureView;
import com.stark.pmu.IPhotoMovieView;
import com.stark.pmu.PhotoMoviePresenter;
import com.stark.pmu.bean.FilterItem;
import com.stark.pmu.bean.FilterType;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.MusicFilterAdapter;
import flc.ast.adapter.TransferAdapter;
import flc.ast.adapter.VideoMusicAdapter;
import flc.ast.bean.MusicBean;
import flc.ast.bean.TransitionBean;
import flc.ast.databinding.ActivityMusicAlbumBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;

public class MusicAlbumActivity extends BaseAc<ActivityMusicAlbumBinding> implements IPhotoMovieView {

    public static List<String> listPath = new ArrayList<>();
    private PhotoMoviePresenter mMoviePresenter;
    private MusicFilterAdapter filterAdapter;
    private TransferAdapter transferAdapter;
    private VideoMusicAdapter musicAdapter;
    private int filterPos = 0, transferPos = 0, musicPos = 0;


    @Override
    protected int onCreate() {
        return R.layout.activity_music_album;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMusicAlbumBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mDataBinding.rvFilterList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        filterAdapter = new MusicFilterAdapter();
        mDataBinding.rvFilterList.setAdapter(filterAdapter);
        filterAdapter.setOnItemClickListener(this);

        mDataBinding.rvTransferList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        transferAdapter = new TransferAdapter();
        mDataBinding.rvTransferList.setAdapter(transferAdapter);
        transferAdapter.setOnItemClickListener(this);

        mDataBinding.rvMusicList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        musicAdapter = new VideoMusicAdapter();
        mDataBinding.rvMusicList.setAdapter(musicAdapter);
        musicAdapter.setOnItemClickListener(this);

        //设置音乐相册
        if (mMoviePresenter == null) {
            mMoviePresenter = new PhotoMoviePresenter(mContext, this);
            mMoviePresenter.setTransfer(PhotoMovieFactory.PhotoMovieType.SCALE_TRANS);
        }
        mMoviePresenter.attachView(this);
        mMoviePresenter.setPhotos(listPath);
        mDataBinding.ivMusicAlbumSave.setOnClickListener(this);
        mDataBinding.llFilter.setOnClickListener(this);
        mDataBinding.llMusic.setOnClickListener(this);
        mDataBinding.llAnimation.setOnClickListener(this);
        mDataBinding.ivMusicAlbumSave.setOnClickListener(this);
        mDataBinding.ivFilterCancel.setOnClickListener(this);
        mDataBinding.ivFilterConfirm.setOnClickListener(this);
        mDataBinding.ivAnimationCancel.setOnClickListener(this);
        mDataBinding.ivAnimationConfirm.setOnClickListener(this);
        mDataBinding.ivMusicCancel.setOnClickListener(this);
        mDataBinding.ivMusicConfirm.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llFilter://滤镜
                mDataBinding.llMusicAlbumBottom.setVisibility(View.GONE);
                mDataBinding.llMusicAlbumFilter.setVisibility(View.VISIBLE);
                break;
            case R.id.ivFilterConfirm:
            case R.id.ivFilterCancel:
                mDataBinding.llMusicAlbumBottom.setVisibility(View.VISIBLE);
                mDataBinding.llMusicAlbumFilter.setVisibility(View.GONE);
                break;
            case R.id.llAnimation://动画
                mDataBinding.llMusicAlbumBottom.setVisibility(View.GONE);
                mDataBinding.llMusicAlbumAnimation.setVisibility(View.VISIBLE);
                break;
            case R.id.ivAnimationConfirm:
            case R.id.ivAnimationCancel:
                mDataBinding.llMusicAlbumBottom.setVisibility(View.VISIBLE);
                mDataBinding.llMusicAlbumAnimation.setVisibility(View.GONE);
                break;
            case R.id.llMusic://音乐
                mDataBinding.llMusicAlbumBottom.setVisibility(View.GONE);
                mDataBinding.llMusicAlbumMusic.setVisibility(View.VISIBLE);
                break;
            case R.id.ivMusicCancel:
            case R.id.ivMusicConfirm:
                mDataBinding.llMusicAlbumBottom.setVisibility(View.VISIBLE);
                mDataBinding.llMusicAlbumMusic.setVisibility(View.GONE);
                break;
            case R.id.ivMusicAlbumSave://保存
                save();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == filterAdapter) {
            filterAdapter.getItem(filterPos).setSelected(false);
            filterPos = position;
            filterAdapter.getItem(position).setSelected(true);
            filterAdapter.notifyDataSetChanged();
            //设置滤镜
            mMoviePresenter.setFilter(filterAdapter.getItem(position));
        } else if (adapter == transferAdapter) {
            transferAdapter.getItem(transferPos).setSelector(false);
            transferPos = position;
            transferAdapter.getItem(position).setSelector(true);
            transferAdapter.notifyDataSetChanged();
            //设置转场
            mMoviePresenter.setTransfer(transferAdapter.getItem(position).getType());
        } else if (adapter == musicAdapter) {
            musicAdapter.getItem(musicPos).setSelected(false);
            musicAdapter.getItem(position).setSelected(true);
            musicPos = position;
            musicAdapter.notifyDataSetChanged();
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + musicAdapter.getItem(position).getMusicUrl());
            setMusic(uri);
        }
    }

    @Override
    protected void initData() {
        //滤镜数据
        List<FilterItem> listFilter = new ArrayList<>();
        listFilter.add(new FilterItem(R.drawable.filter_1, getString(R.string.filter_1), FilterType.NONE));
        listFilter.add(new FilterItem(R.drawable.filter_2, getString(R.string.filter_2), FilterType.GRAY));
        listFilter.add(new FilterItem(R.drawable.filter_3, getString(R.string.filter_3), FilterType.SNOW));
        listFilter.add(new FilterItem(R.drawable.filter_4, getString(R.string.filter_4), FilterType.KUWAHARA));
        listFilter.add(new FilterItem(R.drawable.filter_5, getString(R.string.filter_5), FilterType.LUT1));
        listFilter.add(new FilterItem(R.drawable.filter_6, getString(R.string.filter_6), FilterType.LUT2));
        listFilter.add(new FilterItem(R.drawable.filter_7, getString(R.string.filter_7), FilterType.CAMEO));
        listFilter.add(new FilterItem(R.drawable.filter_8, getString(R.string.filter_8), FilterType.LUT3));
        listFilter.add(new FilterItem(R.drawable.filter_9, getString(R.string.filter_9), FilterType.LUT5));
        listFilter.add(new FilterItem(R.drawable.filter_10, getString(R.string.filter_10), FilterType.LUT4));
        listFilter.get(0).setSelected(true);
        mDataBinding.rvFilterList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        filterAdapter = new MusicFilterAdapter();
        filterAdapter.setList(listFilter);
        mDataBinding.rvFilterList.setAdapter(filterAdapter);
        filterAdapter.setOnItemClickListener(this);
        //动画数据
        List<TransitionBean> listTransfer = new ArrayList<>();
        listTransfer.add(new TransitionBean(R.drawable.azhuanhuan, PhotoMovieFactory.PhotoMovieType.SCALE_TRANS, false));
        listTransfer.add(new TransitionBean(R.drawable.achuangkou, PhotoMovieFactory.PhotoMovieType.WINDOW, false));
        listTransfer.add(new TransitionBean(R.drawable.adiejia, PhotoMovieFactory.PhotoMovieType.GRADIENT, false));
        listTransfer.add(new TransitionBean(R.drawable.ashangxia, PhotoMovieFactory.PhotoMovieType.VERTICAL_TRANS, false));
        listTransfer.add(new TransitionBean(R.drawable.azuoyou, PhotoMovieFactory.PhotoMovieType.HORIZONTAL_TRANS, false));
        listTransfer.add(new TransitionBean(R.drawable.asuof, PhotoMovieFactory.PhotoMovieType.SCALE, false));
        listTransfer.get(0).setSelector(true);
        transferAdapter.setList(listTransfer);
        //音乐
        List<MusicBean> listMusic = new ArrayList<>();
        String[] name = getResources().getStringArray(R.array.music_name);
        listMusic.add(new MusicBean(R.raw.music1, name[0], R.drawable.ayy1, false));
        listMusic.add(new MusicBean(R.raw.music2, name[1], R.drawable.ayy2, false));
        listMusic.add(new MusicBean(R.raw.music3, name[2], R.drawable.ayy3, false));
        listMusic.add(new MusicBean(R.raw.music4, name[3], R.drawable.ayy4, false));
        listMusic.add(new MusicBean(R.raw.music5, name[4], R.drawable.ayy5, false));
        musicAdapter.setList(listMusic);
    }

    private void save() {
        mMoviePresenter.saveVideo(new PhotoMoviePresenter.ISaveCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onProgress(int percent) {
                showDialog(getString(R.string.save_ing) + percent + getString(R.string.unit_percent));
            }

            @Override
            public void onEnd(boolean success, String filePath) {
                dismissDialog();
                if (success) {
                    FileP2pUtil.copyPrivateVideoToPublic(mContext, filePath);//保存到手机相册
                    String path = PathUtils.getFilesPathExternalFirst() + Content.myMusicAlbum + "/" + FileUtils.getFileName(filePath);
                    FileUtils.move(filePath, path);
                    ToastUtils.showShort(getString(R.string.save_suc));
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(HomeActivity.class);
                        }
                    }, 1000);
                } else {
                    ToastUtils.showShort(getString(R.string.save_failure));
                }
            }
        });
    }

    /**
     * 设置音乐
     *
     * @param musicUri
     */
    protected void setMusic(Uri musicUri) {
        if (musicUri == null) return;
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                mMoviePresenter.setMusic(musicUri);
                emitter.onNext(true);
            }

            @Override
            public void accept(Boolean obj) {
                mMoviePresenter.setTransfer(mMoviePresenter.getMovieType());
                dismissDialog();
            }
        });
    }

    @NonNull
    @Override
    public GLTextureView getGLTextureView() {
        return mDataBinding.glTextureView;
    }

    @Override
    public void onPreparingPhoto(float progress) {

    }

    @Override
    public void onPreparedPhoto(int prepared, int total) {

    }

    @Override
    public void onPreparedError() {

    }
}